package it.antonio.ilpizze.ioc.util;

import it.antonio.ilpizze.ioc.DecorationFactory;
import it.antonio.ilpizze.ioc.Dependencies;
import it.antonio.ilpizze.reflection.MethodAccessor;

public abstract class MethodMatcher extends Matcher<MethodAccessor>{

	public <T> DecorationFactory<T> autowire() {
		return DecorationFactory.methods(this, new Injector<MethodAccessor>() {

			@Override
			public void inject(MethodAccessor method, Object instance, Dependencies dep) {
				Class<?>[] parameterKeys = method.getParameterTypes();
				Object[] parameters = new Object[parameterKeys.length];
				for(int i = 0; i < parameterKeys.length; i++){
					parameters[i] = dep.get(parameterKeys[i]);
				}
				method.invoke(instance, parameters);
			}
		});
		
	}

	public <T> DecorationFactory<T> inject(final Object key) {
		return DecorationFactory.methods(this, new Injector<MethodAccessor>() {

			@Override
			public void inject(MethodAccessor method, Object instance, Dependencies dep) {
				method.invoke(instance);
			}
		});
		
	}

	public <T> DecorationFactory<T> inject(Injector<MethodAccessor> injector) {
		return DecorationFactory.methods(this, injector);
	}

}
